package dao;

import java.io.Serializable;
import java.util.List;

import tables.FlightTable;

import com.epam.grsulab.webapi.dao.AbstractDao;
import entity.Flight;

public class FlightDao extends AbstractDao<FlightTable, Flight> {

	public FlightDao(final String rootFolderPath) {
		super(rootFolderPath);
	}

	@Override
	public void saveNew(final Flight newFlight) {
		// set ID
		newFlight.setId(getNextId());
		// get existing data
		final FlightTable flightTable = deserializeFromXml();
		// add new row
		flightTable.getRows().add(newFlight);
		// save data
		serializeToXml(flightTable);
	}

	@Override
	public void update(final Flight entity) {
		// get existing data
		final FlightTable flightTable = deserializeFromXml();
		// find by ID
		for (final Flight row : flightTable.getRows()) {
			if (row.getId().equals(entity.getId())) {
				// found!!!
				// copy data
				row.SetRoute(entity.GetRoute());
				row.SetTime(entity.GetTime());
				break;
			}
		}
		// save updated table
		serializeToXml(flightTable);
	}

	@Override
	public Flight get(final Serializable id) {
		// get existing data
		final FlightTable circleTable = deserializeFromXml();
		// find by ID
		for (final Flight row : circleTable.getRows()) {
			if (row.getId().equals(id)) {
				return row;
			}
		}
		return null;
	}

	@Override
	public List<Flight> getAll() {
		// get existing data
		final FlightTable flightTable = deserializeFromXml();
		return flightTable.getRows();
	}

	@Override
	public void delete(final Serializable id) {
		// get existing data
		final FlightTable flightTable = deserializeFromXml();
		// find by ID
		Flight toBeDeleted = null;
		for (final Flight row : flightTable.getRows()) {
			if (row.getId().equals(id)) {
				// found!!!
				toBeDeleted = row;
				break;
			}
		}
		// remove from list
		flightTable.getRows().remove(toBeDeleted);
		// save updated table
		serializeToXml(flightTable);
	}

	@Override
	protected Class<FlightTable> getTableClass() {
		return FlightTable.class;
	}
}
